﻿using System;
using System.Collections.Generic;
using Color = System.Drawing.Color;
using SwinGame;
using System.Diagnostics;

namespace Gundam_2._0_Alpha.src
{
    public class Character: DestroyableObject
    {
        private Modifier statModifier = new Modifier(1.0f,1.0f,6.0f,0.95f);
        private int hoverMove = 0, shootOffsetX, shootOffsetY, stateRow;
        private bool[] lockoutTiers = new bool[9]; // 1-8 are differet tiers
        private AbilitySet currentSet;
        private bool hover = false, buffLockout, globalLockout = false;
        private GlobalConst.SpriteState state;
        private List<BuffAction> buffs = new List<BuffAction>();
        private AI ai;
        
        public bool BuffLockout
        {
            get { return buffLockout; }
            set { buffLockout = value; }
        }

        public int StateRow
        {
            get { return stateRow; }
            set { stateRow = value; }
        }

        public List<BuffAction> Buffs
        {
            get { return buffs; }
            set { buffs = value; }
        }

        public GlobalConst.SpriteState State
        {
            get { return state; }
            set { state = value; }
        }

        public int offSetSpriteRow()
        {
            switch (state)
            {
                case GlobalConst.SpriteState.normalWest: return stateRow*0;
                case GlobalConst.SpriteState.normalEast: return stateRow*1;
                case GlobalConst.SpriteState.transAmWest: return stateRow*2;
                case GlobalConst.SpriteState.transAmEast: return stateRow*3;
                    
            }
            return 0;
        }

        public int DirectionMultiplier
        {
            get
            {
                if (state == GlobalConst.SpriteState.normalEast || state == GlobalConst.SpriteState.transAmEast)
                    return 1;
                else
                    return -1;
            }
        }

        public int ShootOffsetX()
        {
            if (State == GlobalConst.SpriteState.normalWest || State == GlobalConst.SpriteState.transAmWest)
                return shootOffsetX;
            else
                return objectSprite.Width-shootOffsetX;
        }

        public int ShootOffsetY
        {
            get { return shootOffsetY; }
            set { shootOffsetY = value; }
        }

        public bool GlobalLockout
        {
            get { return globalLockout; }
            set { globalLockout = value; }
        }

        public bool[] LockoutTiers
        {
            get { return lockoutTiers; }
            set { lockoutTiers = value; }
        }
        public AbilitySet CurrentSet
        {
            get { return currentSet; }
            set { currentSet = value; }
        }

        public Modifier StatModifier
        {
            get { return statModifier; }
            set { statModifier = value; }
        } 

        public Character(string name, Sprite gundamSprite,int shootOffSetX,int shootOffsetY, int health, AI ai)
            :base(name,health,gundamSprite)
        {
            this.shootOffsetX = shootOffSetX;
            this.shootOffsetY = shootOffsetY;
            this.ai = ai;
            for (int i = 0; i < lockoutTiers.Length; i++)
                lockoutTiers[i] = false;
        }

        public override void Update(GameController gc)
        {
            Debug.WriteLine("Starting update");
            ai.DoAction(this, gc);
            ControlHovering();

            foreach (BuffAction b in buffs)
            {
                if (!b.IsActive)
                {
                    buffs.Remove(b);
                    return;
                }
                b.DoAction(gc);
            }
            Debug.WriteLine("Ending update");
        }

        public override void Draw()
        {
            objectSprite.Draw();
            objectSprite.UpdateAnimation();
            ai.Draw(this);
            DrawHealth();
        }

        private void ControlHovering()
        {
            if (!hover)
            {
                objectSprite.Y += 0.5f;
                ++hoverMove;
                if (hoverMove == 40) hover = true;
            }
            else
            {
                objectSprite.Y -= 0.5f;
                --hoverMove;
                if (hoverMove == 0) hover = false;
            }
        }

        private void DrawHealth()
        {
            Text.DrawText(Name, Color.Red,GlobalConst.F10, (int)objectSprite.X + objectSprite.Width / 2 - 50, (int)objectSprite.Y);

            Text.DrawText((int)(((float)health / (float)maxHealth) * 100f)+"%", Color.Red, GlobalConst.F10, 
                (int)objectSprite.X + objectSprite.Width / 2 - 50, (int)objectSprite.Y+15);

            Graphics.FillRectangle(Color.Red, (int)objectSprite.X + objectSprite.Width / 2 - 50,
               (int)objectSprite.Y + 30, (int)(((float)health/(float)maxHealth)*100f), 5);
        }


    
    }
}
